<template>
  <div class="app-container">
    <div class="tab">
      <div>
        <router-link to="materialPlay">
          采购计划管理
        </router-link>
      </div>
      <div>
        <router-link to="materialInfo">
          采购信息管理
        </router-link>
      </div>
      <div class="active">
        <router-link to="materiaLdepot">
          仓库管理
        </router-link>
      </div>
      <div>
        <router-link to="materialJunk">
          残料处理管理
        </router-link>
      </div>
    </div>
    <div class="createManager">
      <router-link to="materialPlayCreate">
        <div style="margin: 0 0 0 -5px;" class="addbtn">新增</div>
      </router-link>
    </div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按操作人搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          width="70px"
        >
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="采购材料名称"
        />
        <el-table-column
          prop="type"
          align="center"
          label="种类"
        />
        <el-table-column
          align="center"
          prop="model"
          label="型号"
        />
        <el-table-column
          align="center"
          prop="number"
          label="数量"
        />
        <el-table-column
          align="center"
          prop="company"
          label="单位"
        />
        <el-table-column
          align="center"
          prop="controlUnitPrice"
          label="控制单价"
        />
        <el-table-column
          align="center"
          prop="controlPriceSum"
          label="控制价合计"
        />
        <el-table-column
          align="center"
          label="创建时间"
        >
          <template slot-scope="{row}">
            <span :title="row.createAt">{{ row.createAt }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          align="center"
          prop="updateAt"
          label="修改时间"
        /> -->
        <el-table-column
          align="center"
          label="变更"
        >
          <template slot-scope="{row}">
            <span style="color:#F7B500;cursor: pointer;" title="变更" @click="dialogTop('updata',row)">变更</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="100px"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <div title="详情">
                <router-link :to="{ path: 'materialPlayDetail', query: { id: row.id }}">
                  <img src="@/assets/images/detail.png" alt="">
                </router-link>
              </div>
              <!-- <div title="编辑">
                <router-link :to="{ path: 'edgeProgress', query: { id: row.id }}">
                  <img src="@/assets/images/edit.png" alt="">
                </router-link>
              </div> -->
              <div title="删除" @click="deleteDialog(row)">
                <img src="@/assets/images/delete-img.png" alt="">
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>
      <delete-dialog
        dislog-name="采购计划管理数据"
        :dialog-del-visible="dialogDelVisible"
        :title="'采购计划'"
        @funcancl="funcancl"
        @func="deleteData"
      />
    </div>

    <!-- 添加,编辑 -->
    <el-dialog :title="diaDelOrAdd?'变更':'编辑'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" custom-class="customClass">
      <el-form ref="addForm" :model="addForm" :rules="rules" auto-complete="on" label-position="left" style="width: 97%;">
        <el-form-item label="采购数量：" :label-width="formLabelWidth" prop="numberChange">
          <el-input
            v-model="addForm.numberChange"
            placeholder="请输入采购数量"
            name="numberChange"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="使用部位：" :label-width="formLabelWidth" prop="technicalRequirementChange">
          <el-input
            v-model="addForm.technicalRequirementChange"
            placeholder="请输入使用部位"
            name="technicalRequirementChange"
            tabindex="2"
            autocomplete="off"
          />
        </el-form-item>
        <!-- <el-form-item label="变更单合计：" :label-width="formLabelWidth" prop="changeSum">
          <el-input
            v-model="addForm.changeSum"
            placeholder="请输入变更单合计"
            name="changeSum"
            tabindex="3"
            autocomplete="off"
          />
        </el-form-item> -->
      </el-form>
      <div class="footer">
        <div class="cancel" @click="dialogTop('cancel')">
          <span>取消</span>
        </div>
        <div v-if="diaDelOrAdd" class="sure" @click="createData">
          <span>确认</span>
        </div>
        <!-- <div v-else class="sure" @click="updataData">
          <span>确认</span>
        </div> -->
      </div>
    </el-dialog>

  </div>

</template>

<script>
import Pagination from '@/components/pagination'
import DeleteDialog from '@/components/dialog/delete'
import { planList, planDelete, createPlanChange } from '@/api/Material'
export default {
  name: 'MateriaLdepot',
  components: {
    Pagination,
    DeleteDialog
  },
  data() {
    return {
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10
      },
      addForm: {
        purchaseId: '',
        numberChange: '',
        technicalRequirementChange: '',
        changeSum: null
      },
      deleteObiect: {},
      formLabelWidth: '120px',
      lablePosition: 'left',
      diaDelOrAdd: false,
      dialogFormVisible: false,
      dialogDelVisible: false,
      rules: {
        numberChange: [
          { required: true, message: '必填', trigger: 'change' },
          { pattern: /(^[1-9]{1}[0-9]*$)|(^[0-9]*\.[0-9]{2}$)/, message: '只能输入数字或者数字加小数点后两位' }
        ],
        technicalRequirementChange: [
          { required: true, message: '必填', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    funcancl(val) {
      this.dialogDelVisible = false
    },
    // 获取数据
    async getList() {
      const res = await planList(this.listQuery)
      this.listLoading = false
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
      }
    },
    clean() {
      this.addForm = {
        numberChange: '',
        technicalRequirementChange: '',
        changeSum: null
      }
    },
    // 删除
    deleteDialog(row) {
      this.dialogDelVisible = true
      this.deleteObiect = row
    },
    async deleteData(row) {
      const id = this.deleteObiect.id
      const res = await planDelete([id])
      if (res.code === 200) {
        this.getList()
        this.dialogDelVisible = false
        this.$message({
          type: 'success',
          message: '删除成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.dialogDelVisible = false
      }
    },
    // 变更
    dialogTop(val, row) {
      this.clean()
      if (val === 'cancel') {
        this.$refs.addForm.resetFields()
        this.diaDelOrAdd = false
        this.dialogFormVisible = false
      } else {
        this.addForm.purchaseId = row.id
        this.diaDelOrAdd = true
        this.dialogFormVisible = true
      }
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    createData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await createPlanChange(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: '变更成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            console.log(res)
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/variables';
.app-container{
  .tab{
    font-size:14px;
    font-weight: 400;
    color: #ffffff;
    display: flex;
    div{
      cursor: pointer;
      margin:10px 5px 0 20px;
    }
    .active{
      padding-bottom: 7px;
      border-bottom: 1.5px solid #F7B500;
    }
  }
  .createManager{
    position: absolute;
    margin: 13px 0 0 26px;
    font-size:$fontSize14;
    font-weight:400;
    color:rgba(255,255,255,0.65);
   button{
      background:$btnactiveColor !important;
   }
  }
  .content{
    .operate{
      display: flex;
      font-size:$fontSize12;
      font-weight:400;
      color:rgba(247,181,0,1);
      div{
        cursor: pointer;
        flex: 1;
      }
    }
  }
}
</style>
